using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Network;
using System;
namespace MicroMsg.Scene
{
	public class ContextMgrBase<TContext> where TContext : IContextBase
	{
		private const string TAG = "ContextMgrBase";
		protected Queue mContextQueue = new Queue();
		protected object mQueueLock = new object();
		private EventWatcher mWatcher;
		public ContextMgrBase()
		{
			if (this.mWatcher == null)
			{
				this.mWatcher = new EventWatcher(this, null, new EventHandlerDelegate(this.HandlerOnLogout));
				EventCenter.registerEventWatcher(EventConst.ON_ACCOUNT_LOGOUT, this.mWatcher);
			}
		}
		private void HandlerOnLogout(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			lock (this.mQueueLock)
			{
				Log.smethod_5("ContextMgrBase", "clearn task on logout.. " + this.mContextQueue.size());
				this.mContextQueue.reset();
			}
		}
		public int getCount()
		{
			int result;
			lock (this.mQueueLock)
			{
				result = this.mContextQueue.size();
			}
			return result;
		}
		public TContext getFirstContextNeedHandle()
		{
			TContext result;
			lock (this.mQueueLock)
			{
				if (this.mContextQueue.isEmpty())
				{
					result = default(TContext);
				}
				else
				{
					for (ListNode next = this.mContextQueue._head._next; next != null; next = next._next)
					{
						TContext tContext = (TContext)((object)next._obj);
						if (tContext != null && tContext.needToHandle())
						{
							result = tContext;
							return result;
						}
					}
					result = default(TContext);
				}
			}
			return result;
		}
		public int countRunningContext()
		{
			int result;
			lock (this.mQueueLock)
			{
				if (this.mContextQueue.isEmpty())
				{
					result = 0;
				}
				else
				{
					int num = 0;
					for (ListNode next = this.mContextQueue._head._next; next != null; next = next._next)
					{
						TContext tContext = (TContext)((object)next._obj);
						if (tContext != null && tContext.isRunning())
						{
							num++;
						}
					}
					result = num;
				}
			}
			return result;
		}
		public int clearnFinishedContext()
		{
			int result;
			lock (this.mQueueLock)
			{
				if (this.mContextQueue.isEmpty())
				{
					result = 0;
				}
				else
				{
					int num = 0;
					ListNode next = this.mContextQueue._head._next;
					ListNode listNode = this.mContextQueue._head;
					while (next != null)
					{
						TContext tContext = (TContext)((object)next._obj);
						if (tContext != null && !tContext.needToClean())
						{
							listNode = next;
						}
						else
						{
							listNode._next = next._next;
							this.mContextQueue._size--;
							num++;
						}
						next = next._next;
					}
					this.mContextQueue._tail = listNode;
					result = num;
				}
			}
			return result;
		}
		public void putToHead(TContext context)
		{
			lock (this.mQueueLock)
			{
				this.mContextQueue.putToHead(context, context.GetHashCode());
			}
		}
		public void remove(TContext context)
		{
			lock (this.mQueueLock)
			{
				this.mContextQueue.remove(context.GetHashCode());
			}
		}
		public void putToTail(TContext context)
		{
			lock (this.mQueueLock)
			{
				this.mContextQueue.putToTail(context, context.GetHashCode());
			}
		}
		public void moveToTail(TContext context)
		{
			lock (this.mQueueLock)
			{
				this.mContextQueue.moveToTail(context.GetHashCode());
			}
		}
	}
}
